package com.wwz.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wwz.pojo.Rent;
import com.wwz.pojo.query.RentQuery;
import com.wwz.service.IRentService;
import com.wwz.util.EcoBootException;
import com.wwz.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


/**
 * 租赁控制层
 * @date 2022/03/26 12:04:59
 */
@RestController
@RequestMapping("/manage/rent")
@Api(tags = "租赁控制层")
public class RentController {

    @Resource
    private IRentService rentService;

    @ApiOperation("客户列表")
    @GetMapping("/toPage")
    public Result toPage(RentQuery query){
        IPage<Rent> rentIPage = rentService.toPage(query);
        return Result.ok(rentIPage);
    }

    @ApiOperation("删除租赁信息")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        rentService.removeById(id);
        return Result.ok("ok");
    }

    @ApiOperation("添加或修改客户信息")
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Rent rent){

        if (StrUtil.isAllEmpty(rent.getRentUser()
                ,rent.getHouseMaster()
                ,rent.getHouseMaster()
                ,rent.getPay()
        )){
            throw new EcoBootException("信息为空");
        }
        rentService.addOrUpdate(rent);
        return Result.ok("ok");
    }

    @ApiOperation("导出租赁信息")
    @GetMapping("/output/{id}")
    public void outPut(HttpServletResponse response, @PathVariable String id){
        if (StrUtil.isEmpty(id)){
            throw new EcoBootException("id不能为空");
        }
        rentService.export(response,id);
    }
}
